const express=require("express");
const router=express.Router();
const request=require("request");
const cheerio=require("cheerio");

router.get("/qianqianmusic",(req,res)=>{
	var musicArr=[];
	request("http://www.duba.com/music_index.html",function(error,response,body){
		if(!error && response.statusCode==200){
			$=cheerio.load(body);//当前的$他是一个拿到了整个body前端选择器
			const addr=$('.g-ranking dd')
			addr.map((i,val)=>{
				var obj={};
				obj.musiclink=$(val).find('.music').attr('href');
				obj.musicname=$(val).find('.music').text();
				obj.musicauthor=$(val).find('.name').text()
				if(obj.musicauthor==""||obj.musiclink==""||obj.musicname=="")
				{
					return;
				}else{
					musicArr.push(obj);
				}
			})
			if(musicArr.length>0)
			{
				res.json(musicArr);
			}
		}
	})
})
router.get("/maoyan",(req,res)=>{
	var maoyanArr=[];
	request("https://maoyan.com/films",function(error,response,body){
		if(!error && response.statusCode==200){
			$=cheerio.load(body);//当前的$他是一个拿到了整个body前端选择器
			const item=$('.movies-channel dd')
			item.map((i,val)=>{
				var obj={};
				obj.maoyanlink='https://maoyan.com'+$(val).find('.movie-item').children('a').attr('href');
				obj.maoyantupian=$(val).find('.movie-hover-img').attr('src');
				obj.maoyantitle=$(val).find('.movie-item-title').children('a').text()
				obj.maoyanpingfen=$(val).find('.channel-detail-orange').last().text()
				maoyanArr.push(obj)
			})
			if(maoyanArr.length>0)
			{
				res.json(maoyanArr);
			}
		}
	})
})

router.get("/tupian",(req,res)=>{
	var tupianArr=[];
	request({url:"https://www.mmonly.cc/mmtp/qcmn/",encoding:"utf-8"},function(error,response,body){
		if(!error && response.statusCode==200){
			$=cheerio.load(body);//当前的$他是一个拿到了整个body前端选择器
			const item=$('.masonry .masonry-brick')
			item.map((i,val)=>{
				var obj={}
				obj.tupianLink=$(val).find('.ABox').children('a').attr('href');
				obj.tupianImg=$(val).find('.ABox').children('a').children('img').attr('src');
				tupianArr.push(obj)
			})
			if(tupianArr.length>0){
				res.json(tupianArr)
			}
		}
	})
})
module.exports = router;